﻿//-----------------------------------------------------------------------
// <copyright file="FilterInfoCache.cs" company="Microsoft Corporation">
// Copyright (c) Microsoft Corporation.
// All rights reserved.
// THIS CODE AND INFORMATION ARE PROVIDED "AS IS" WITHOUT WARRANTY OF ANY 
// KIND, EITHER EXPRESSED OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE
// IMPLIED WARRANTIES OF MERCHANTABILITY AND/OR FITNESS FOR A
// PARTICULAR PURPOSE.
// This code released under the terms of the 
// Microsoft Public License (MS-PL, http://opensource.org/licenses/ms-pl.html.)
// </copyright>
// <author>Yogesh M Joshi</author>
// <email>ymjoshi@live.com</email>
// <date>16-May-2011</date>
//-----------------------------------------------------------------------

namespace SmartTag.Common.IFilter
{
    using System.Collections.Generic;

    /// <summary>
    /// Thic class represents the Cache entries for filters
    /// </summary>
    internal static class FilterInfoCache
    {
        #region "Private Fields"

        /// <summary>
        /// Internal dictionary containing filter information
        /// </summary>
        private static Dictionary<string, FilterInfo> filterInfoCache;

        #endregion

        /// <summary>
        /// Initializes static members of the FilterInfoCache class
        /// </summary>
        static FilterInfoCache()
        {
            filterInfoCache = new Dictionary<string, FilterInfo>();
        }

        /// <summary>
        /// Add new entry inside cache
        /// </summary>
        /// <param name="extenstion">Extension of file</param>
        /// <param name="dllName">Name of dll</param>
        /// <param name="className">Name of the class</param>
        /// <returns>Filter information object</returns>
        public static FilterInfo AddEntry(string extenstion, string dllName, string className)
        {
            FilterInfo filterInfo = new FilterInfo
                {
                    DllName = dllName,
                    IPersistClass = className
                };

            if (filterInfoCache.ContainsKey(dllName))
            {
                filterInfoCache[dllName] = filterInfo;
                return filterInfo;
            }

            filterInfoCache.Add(extenstion, filterInfo);
            return filterInfo;
        }

        /// <summary>
        /// Get the filter enty from cache
        /// </summary>
        /// <param name="extension">Name of dll</param>
        /// <returns>Filter information object</returns>
        public static FilterInfo GetEntry(string extension)
        {
            FilterInfo cacheEntry = null;
            filterInfoCache.TryGetValue(extension, out cacheEntry);
            return cacheEntry;
        }

        /// <summary>
        /// This class represents single entry in cache
        /// </summary>
        internal class FilterInfo
        {
            /// <summary>
            /// Gets or sets the name of dll
            /// </summary>
            public string DllName
            {
                get;
                set;
            }

            /// <summary>
            /// Gets or sets the name of Persist Class
            /// </summary>
            public string IPersistClass
            {
                get;
                set;
            }
        }
    }
}
